package com.hua.base;

public class DeepCloneDemo implements Cloneable{

    @Override //深拷贝 ，拷贝对象和原始对象的引用类型引用不同对象。
    protected DeepCloneDemo clone() throws CloneNotSupportedException {
        DeepCloneDemo result = (DeepCloneDemo) super.clone();
        result.arr = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            result.arr[i] = arr[i];
        }
        return result;
    }

    private int[] arr;
    public DeepCloneDemo(){
        arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
    }
    public void set(int index,int value){
        arr[index] = value;
    }
    public int get(int index){
        return arr[index];
    }


    public static void main(String[] args) {
        DeepCloneDemo e1 = new DeepCloneDemo();
        DeepCloneDemo e2 = null;

        try {
            e2 = e1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        e1.set(2,2223);
        System.out.println(e2.get(2));//2  拷贝对象和原始对象的引用类型引用不同对象。
    }
}
